{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MySQL5.6\n",
    "\n",
    "## dockerhostの設定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%%bash\n",
    "### mysqlクライアントのインストール(CentOS7)\n",
    "sudo yum install -y http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm\n",
    "sudo yum install -y mysql-community-client.x86_64"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## コンテナの作成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        17 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        18 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES\n",
      "REPOSITORY                    TAG                      IMAGE ID            CREATED             SIZE\n",
      "ftakao2007/mail_centos7       virtual_for_jorurimail   35e0eebeeb79        17 hours ago        657MB\n",
      "ftakao2007/mail_centos7       latest                   0733ee002c86        18 hours ago        656MB\n",
      "ftakao2007/openldap_centos7   latest                   572b38993fa4        2 days ago          596MB\n",
      "ftakao2007/mysql5.6_centos7   latest                   09a773ad8e34        4 days ago          2.16GB\n",
      "ftakao2007/centos7            latest                   0fede72fa1fe        4 days ago          488MB\n",
      "CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS                  PORTS                                          NAMES\n",
      "4c0ae849ad6a        ftakao2007/mysql5.6_centos7:latest   \"sh -c 'exec /usr/...\"   1 second ago        Up Less than a second   0.0.0.0:3306->3306/tcp, 0.0.0.0:2222->22/tcp   mysql5.6_centos7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating mysql5.6_centos7\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "container=\"mysql5.6_centos7\"\n",
    "docker_compose_file=\"docker-compose_${container}.yml\"\n",
    "mysql_port=\"3306\"\n",
    "ssh_port=\"2222\"\n",
    "\n",
    "### イメージ、コンテナが無い状態の確認\n",
    "docker images\n",
    "docker ps -a\n",
    "\n",
    "### docker-composeディレクトリに移動\n",
    "#work_dir=\"/home/jsd/work/build/docker-compose\"\n",
    "work_dir=\"${PYENV_DIR}/work/build/docker-compose\"\n",
    "cd ${work_dir}\n",
    "\n",
    "### docker-composeファイルの作成\n",
    "cat <<- EOS > ${docker_compose_file}\n",
    "${container}:\n",
    "   image: ftakao2007/mysql5.6_centos7:latest\n",
    "   container_name: ${container}\n",
    "   privileged: true\n",
    "   tty: true\n",
    "   ports:\n",
    "     - \"${mysql_port}:3306\"\n",
    "     - \"${ssh_port}:22\"\n",
    "   #command: sh -c '/sbin/init'\n",
    "   command: sh -c 'exec /usr/sbin/init && systemctl restart sshd.service && systemctl restart mysqld.service'\n",
    "EOS\n",
    "\n",
    "### コンテナの作成\n",
    "docker-compose -f ${docker_compose_file} up -d\n",
    "\n",
    "### イメージ、コンテナの確認\n",
    "docker images\n",
    "docker ps -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CURDATE()\n",
      "2017-07-18\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### mysql接続確認\n",
    "mysql -ujoruri -p'pass' -h 127.0.0.1 -P 3306 -e \"SELECT CURDATE();\" 2> /dev/null"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VIPの付与"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)\n",
      "        RX packets 23  bytes 2020 (1.9 KiB)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 13  bytes 1090 (1.0 KiB)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 0  bytes 0 (0.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### ネットワーク情報の確認\n",
    "container=\"mysql5.6_centos7\"\n",
    "docker exec ${container} ifconfig -a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0\n",
      "        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)\n",
      "        RX packets 23  bytes 2020 (1.9 KiB)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 13  bytes 1090 (1.0 KiB)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n",
      "        inet 172.17.33.11  netmask 255.255.0.0  broadcast 172.17.255.255\n",
      "        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)\n",
      "\n",
      "lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536\n",
      "        inet 127.0.0.1  netmask 255.0.0.0\n",
      "        loop  txqueuelen 1  (Local Loopback)\n",
      "        RX packets 0  bytes 0 (0.0 B)\n",
      "        RX errors 0  dropped 0  overruns 0  frame 0\n",
      "        TX packets 0  bytes 0 (0.0 B)\n",
      "        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n",
      "\n",
      "CURDATE()\n",
      "2017-07-18\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### VIPを付与する\n",
    "container=\"mysql5.6_centos7\"\n",
    "ip=\"172.17.33.11\"\n",
    "netmask=\"255.255.0.0\"\n",
    "device=\"eth0\"\n",
    "docker exec ${container} ifconfig ${device}:1 ${ip} netmask ${netmask}\n",
    "\n",
    "### ネットワーク確認\n",
    "docker exec ${container} ifconfig -a\n",
    "\n",
    "### MySQL接続確認\n",
    "mysql -ujoruri -p'pass' -h ${ip} -P 3306 -e \"SELECT CURDATE();\" 2> /dev/null"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
